using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
using Microsoft.Xna.Framework.Media;
using FarseerPhysics;
using HappyFrogXna.ScreenSystem;

namespace HappyFrogXna
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class HappyFrogGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager _graphics;
        ScreenManager _screenManager { get; set; }

        public HappyFrogGame()
        {
            // Initialize Graphics Device Manager
            Window.Title = "Farseer Samples Framework";
            _graphics = new GraphicsDeviceManager(this);
            _graphics.PreferMultiSampling = true;

            _graphics.PreferredBackBufferWidth = 800;
            _graphics.PreferredBackBufferHeight = 480;
            ConvertUnits.SetDisplayUnitToSimUnitRatio(16f);

            // Initialize ContentManager
            Content.RootDirectory = "Content";

#if WINDOWS_PHONE
            _graphics.IsFullScreen = true;
#endif

            // This will display a frame rate counter on the top-left corner
            FrameRateCounter frameRateCounter = new FrameRateCounter(this);
            frameRateCounter.DrawOrder = 101;
            Components.Add(frameRateCounter);

#if WINDOWS_PHONE
            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Extend battery life under lock.
            //InactiveSleepTime = TimeSpan.FromSeconds(1);
#else
            // Frame rate is 40 fps by default for Windows PC.
            TargetElapsedTime = TimeSpan.FromTicks(250000);
#endif
        }

        protected override void LoadContent()
        {
            GolbalXnaGadgetProvider.Instance.SpriteBatch = _screenManager.SpriteBatch;

            base.LoadContent();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // new-up components and add to Game.Components
            _screenManager = new ScreenManager(this);
            Components.Add(_screenManager);

            // game screen
            GolbalXnaGadgetProvider.Instance.Content = this.Content;
            GolbalXnaGadgetProvider.Instance.GraphicsDevice = this.GraphicsDevice;

            GamePlayScreen gamePlayScreen = new GamePlayScreen();
            SceneBuildScreen sceneBuildScreen = new SceneBuildScreen();

            MenuScreen menuScreen = new MenuScreen("Happy Frog");

            menuScreen.AddMenuItem("Main Game", EntryType.Separator, null);
            menuScreen.AddMenuItem("Play Game", EntryType.Screen, gamePlayScreen);
            menuScreen.AddMenuItem("Build Scene", EntryType.Screen, sceneBuildScreen);
            menuScreen.AddMenuItem("Exit Game", EntryType.Separator, null);
            menuScreen.AddMenuItem("Exit", EntryType.ExitItem, null);

            menuScreen.AddScreen = _screenManager.AddScreen;
            menuScreen.RemoveScreen = _screenManager.RemoveScreen;
            menuScreen.Exit = this.Exit;
            menuScreen.ResetElapsedTime = () => { };

            _screenManager.AddScreen(menuScreen);
            //_screenManager.AddScreen(gamePlayScreen);

            base.Initialize();
        }

    }
}
